package com.oydsoft.service.orgmanage.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oydsoft.api.orgmanage.OrgManageControllerApi;
import com.oydsoft.common.model.response.CommonCode;
import com.oydsoft.common.model.response.QueryResponseResult;
import com.oydsoft.common.model.response.QueryResult;
import com.oydsoft.common.model.response.ResponseResult;
import com.oydsoft.common.util.UuidUtils;
import com.oydsoft.common.web.BaseController;
import com.oydsoft.dto.orgmanage.B01;
import com.oydsoft.dto.orgmanage.B01QueryVo;
import com.oydsoft.service.orgmanage.mapper.B01Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 * @version 1.0
 **/
@RestController
public class OrgManageController extends BaseController implements OrgManageControllerApi {

    @Autowired
    B01Mapper b01Mapper;

    @Override
    public QueryResponseResult findList(Integer pageNumber, Integer pageSize, B01QueryVo A01vo) {
        Page<B01> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<B01> queryWrapper = new QueryWrapper<>();

//        queryWrapper.eq();//查询条件
//        List<B01> a = b01Mapper.selectB01IncloudB23();
        Page<B01> a01Page = b01Mapper.selectPage(page, queryWrapper);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(a01Page.getRecords());//数据列表
        queryResult.setTotal(a01Page.getTotal());//数据总记录数
        QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);
        return queryResponseResult;

//        Pageable pageable = PageRequest.of(pageNumber,pageSize);
//        Page<B01> a01Page = b01Mapper.findAll(pageable);//实现自定义条件查询并且分页查询
//
//        QueryResult queryResult = new QueryResult();
//        queryResult.setList(a01Page.getContent());//数据列表
//        queryResult.setTotal(a01Page.getTotalElements());//数据总记录数
//        QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);
//        return queryResponseResult;
    }

    @Override
    public ResponseResult add(B01 b01) {
       if(b01 == null){//抛出异常
//           Exception
       }
       b01.setB01Id(UuidUtils.getUuIdStr());
       b01Mapper.insert(b01);
       return new ResponseResult(CommonCode.SUCCESS);
    }

    @Override
    public B01 findById(String id) {
        B01 b01 = b01Mapper.selectById(id);
            return b01;
    }

    @Override
    public ResponseResult update(String id, B01 b01) {
        B01 b01s = b01Mapper.selectById(id);
        if(b01s!=null){
//            b01.setB01Id(UuidUtils.getUuIdStr());
//
//            QueryWrapper<B01> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("B01_ID",id);
//            b01Mapper.update(b01,queryWrapper);
//            b01Mapper.updateById(b01);

            UpdateWrapper<B01> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("B01_ID",id);

            b01Mapper.update(b01,updateWrapper);
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    @Override
    public ResponseResult delete(String id) {
        B01 b01 = b01Mapper.selectById(id);
        if (b01 != null) {
            b01Mapper.deleteById(id);
            return new ResponseResult(CommonCode.SUCCESS);
        }
            return new ResponseResult(CommonCode.FAIL);

    }
}
